//
//  SEGWKWebNavigationView.m
//  SEGWebView
//
//  Created by 山刘 on 2020/3/10.
//

#import "SEGWKWebNavigationView.h"
#import "NSString+WKAdd.h"

@interface SEGWKWebNavigationView()
@property (nonatomic, strong)UILabel *titleLabel;
@end

@implementation SEGWKWebNavigationView
-(instancetype)initWithFrame:(CGRect)frame{
    if (self == [super initWithFrame:frame]) {
        self.backgroundColor = UIColor.orangeColor;
        [self segSddSubviews];
    }
    return self;
}

-(void)segSddSubviews{
//    [self addSubview:self.closeButton];
    [self addSubview:self.titleLabel];
//    [self addSubview:self.shareButton];
//    [self addSubview:self.reloadButton];
    [self addSubview:self.lineView];
}

-(void)layoutSubviews{
    [super layoutSubviews];
    
    [self.lineView setFrame:CGRectMake(0, CGRectGetHeight(self.frame) - 0.5, CGRectGetWidth(self.frame), 0.5)];
    
//    [self.closeButton sizeToFit];
//    [self.closeButton setFrame:CGRectMake(14.5,
//                                           CGRectGetHeight(self.frame) - CGRectGetHeight(self.closeButton.bounds) - 14.5,
//                                           CGRectGetWidth(self.closeButton.bounds),
//                                           CGRectGetHeight(self.closeButton.bounds))];
//    [self.reloadButton sizeToFit];
//    [self.reloadButton setFrame:CGRectMake(CGRectGetWidth(self.frame) - CGRectGetWidth(self.reloadButton.bounds) - 14.5,
//                                          CGRectGetHeight(self.frame) - CGRectGetHeight(self.reloadButton.bounds) - 14.5,
//                                           CGRectGetWidth(self.reloadButton.bounds),
//                                           CGRectGetHeight(self.reloadButton.bounds))];
//
//    [self.shareButton sizeToFit];
//    [self.shareButton setFrame:CGRectMake(CGRectGetMinX(self.reloadButton.frame) - CGRectGetWidth(self.shareButton.bounds) - 10,
//                                          CGRectGetHeight(self.frame) - CGRectGetHeight(self.shareButton.bounds) - 14.5,
//                                           CGRectGetWidth(self.shareButton.bounds),
//                                           CGRectGetHeight(self.shareButton.bounds))];
    
    if (self.title && self.title.length > 0) {
        CGFloat maxTitleWidth = CGRectGetWidth(self.frame) - 100;
        CGSize size = [self.title sizeForFont:_titleLabel.font size:CGSizeMake(maxTitleWidth,CGFLOAT_MAX) mode:NSLineBreakByTruncatingTail];
        size.width = size.width > maxTitleWidth ? maxTitleWidth : size.width;
        
        self.titleLabel.text = self.title;
        [self.titleLabel setFrame:CGRectMake((CGRectGetWidth(self.frame) - CGRectGetWidth(self.titleLabel.frame))*0.5,
                                             CGRectGetHeight(self.frame) - CGRectGetHeight(self.titleLabel.bounds) - 14.5,
                                             size.width,
                                             size.height)];
        [self.titleLabel setHidden:NO];
    }else{
        [self.titleLabel setFrame:CGRectZero];
        [self.titleLabel setHidden:YES];
    }
}

//-(UIButton *)closeButton
//{
//    if (!_closeButton) {
//        UIImage *image = [UIImage imageNamed:[NSString resourceName:@"close"]];
//        image = [image imageWithRenderingMode:(UIImageRenderingModeAlwaysTemplate)];
//
//        _closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
//        [_closeButton setImage:image forState:UIControlStateNormal];
//    }
//    return _closeButton;
//}
//
//-(UIButton *)shareButton{
//    if (!_shareButton) {
//        UIImage *image = [UIImage imageNamed:[NSString resourceName:@"share"]];
//        image = [image imageWithRenderingMode:(UIImageRenderingModeAlwaysTemplate)];
//
//        _shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
//        [_shareButton setImage:image forState:UIControlStateNormal];
//        [_shareButton setHidden:YES];
//    }
//    return _shareButton;
//}
//
//-(UIButton *)reloadButton{
//    if (!_reloadButton) {
//        UIImage *image = [UIImage imageNamed:[NSString resourceName:@"refresh"]];
//        image = [image imageWithRenderingMode:(UIImageRenderingModeAlwaysTemplate)];
//
//        _reloadButton = [UIButton buttonWithType:UIButtonTypeCustom];
//        [_reloadButton setImage:image forState:UIControlStateNormal];
//    }
//    return _reloadButton;
//}

-(UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.numberOfLines = 1;
        _titleLabel.font = [UIFont fontWithName:@"PingFang SC" size: 16];
    }
    return _titleLabel;
}

-(UIView *)lineView{
    if (!_lineView) {
        _lineView = [[UIView alloc] init];
    }
    return _lineView;
}

-(void)setTitle:(NSString *)title{
    _title = title;
    [self layoutSubviews];
}

-(void)setTitleColor:(UIColor *)titleColor{
    _titleColor = titleColor;
    _titleLabel.textColor = titleColor;
}
@end

